package com.AAL.Action;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.AAL.DAO.PurchaseInvoiceDAO;
import com.AAL.DAO.PurchaseInvoiceDetailDAO;
import com.AAL.DAO.StatusOfInvoiceDAO;
import com.AAL.Model.PurchaseInvoice;
import com.AAL.Model.PurchaseInvoiceDetail;
import com.AAL.Model.StatusOfInvoice;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Loc DV
 * Create date:30.3.2011
 */
public class PurchaseInvoiceAction extends ActionSupport implements
		SessionAware {

	/**
	 * LocDV
	 */
	private static final long serialVersionUID = 1L;

	private PurchaseInvoice purchaseInvoice;
	private StatusOfInvoice statusOfInvoice;
	private PurchaseInvoiceDetail purchaseInvoiceDetail;
	private StatusOfInvoiceDAO lnkStatusOfInvoice;
	private PurchaseInvoiceDAO lnkPurchaseInvoice;
	private PurchaseInvoiceDetailDAO lnkPurchaseInvoiceDetail;
	private List<StatusOfInvoice> lstStatusOfInvoice;
	private List<PurchaseInvoiceDetail> lstPurchaseInvoiceDetail;
	private List<PurchaseInvoice> lstPurchaseInvoice;
	private String status;
	private Timestamp begin;
	private Timestamp end;

	//
	public PurchaseInvoiceAction() {
		this.purchaseInvoice = new PurchaseInvoice();
		this.lnkStatusOfInvoice = new StatusOfInvoiceDAO();
		this.lnkPurchaseInvoiceDetail = new PurchaseInvoiceDetailDAO();
		this.lnkPurchaseInvoice = new PurchaseInvoiceDAO();

	}

	/**
	 * view menu Hoa don va danh sach hoa don theo hop dong
	 */
	public String purchaseByContract() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {

			int purchaseContractID = Integer.parseInt(request.getParameter("ID"));
			this.lstPurchaseInvoice = this.lnkPurchaseInvoice.getLstPurchaseInvoicesByPurchaseContract(purchaseContractID);
			this.lstStatusOfInvoice=this.lnkStatusOfInvoice.getAllStatusOfInvoices();
			
			System.out.println("Contract "+ lstPurchaseInvoice.get(0).getPurchaseInvoiceID());

		} catch (Exception e) {
			// TODO: handle exception
			return ERROR;
		}

		return SUCCESS;

	}
	/**
	 * view tat ca cac hoa don
	 */
	public String allPurchaseInvoice() {

		try {
			this.lstPurchaseInvoice = this.lnkPurchaseInvoice.getLstPurchaseInvoices();
			this.lstStatusOfInvoice=this.lnkStatusOfInvoice.getAllStatusOfInvoices();
			System.out.println(" PurchaseInvoice "+ lstPurchaseInvoice.get(0).getPurchaseInvoiceID());
		} catch (Exception e) {
			// TODO: handle exception
			return ERROR;
		}

		return SUCCESS;
	}

	public String saveOrUpdatePurchase() {
		try {
			// if (null != purchaseInvoice) {
			 System.out.println(purchaseInvoice.getPurchaseInvoiceID());
			// lnkPurchaseInvoice.saveAndUpdatePurchaseInvoice(purchaseInvoice);

			// }
			System.out.println("Insert new Purchase");
			return SUCCESS;
		} catch (Exception e) {
			return ERROR;
		}

	}
	/**
	 * searchPurchaseInvoice
	 */
	public String searchPurchaseInvoice()
	{
		try{
			this.lstPurchaseInvoice=this.lnkPurchaseInvoice.searchPurchaseInvoices(status, begin, end);
			System.out.println("Search PurchaseInvoice"+ status + begin+ end);
			this.lstStatusOfInvoice=this.lnkStatusOfInvoice.getAllStatusOfInvoices();
		}
		catch (Exception e) {
			// TODO: handle exception
			return ERROR;
		}
		return SUCCESS;
	}


	public String execute() {

		return SUCCESS;
	}

	public PurchaseInvoice getPurchaseInvoice() {
		return purchaseInvoice;
	}

	public void setPurchaseInvoice(PurchaseInvoice purchaseInvoice) {
		this.purchaseInvoice = purchaseInvoice;
	}

	public StatusOfInvoiceDAO getLnkStatusOfInvoice() {
		return lnkStatusOfInvoice;
	}

	public void setLnkStatusOfInvoice(StatusOfInvoiceDAO lnkStatusOfInvoice) {
		this.lnkStatusOfInvoice = lnkStatusOfInvoice;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub

	}

	public StatusOfInvoice getStatusOfInvoice() {
		return statusOfInvoice;
	}

	public void setStatusOfInvoice(StatusOfInvoice statusOfInvoice) {
		this.statusOfInvoice = statusOfInvoice;
	}

	public List<StatusOfInvoice> getLstStatusOfInvoice() {
		return lstStatusOfInvoice;
	}

	public void setLstStatusOfInvoice(List<StatusOfInvoice> lstStatusOfInvoice) {
		this.lstStatusOfInvoice = lstStatusOfInvoice;
	}

	public PurchaseInvoiceDetail getPurchaseInvoiceDetail() {
		return purchaseInvoiceDetail;
	}

	public void setPurchaseInvoiceDetail(
			PurchaseInvoiceDetail purchaseInvoiceDetail) {
		this.purchaseInvoiceDetail = purchaseInvoiceDetail;
	}

	public PurchaseInvoiceDAO getLnkPurchaseInvoice() {
		return lnkPurchaseInvoice;
	}

	public void setLnkPurchaseInvoice(PurchaseInvoiceDAO lnkPurchaseInvoice) {
		this.lnkPurchaseInvoice = lnkPurchaseInvoice;
	}

	public PurchaseInvoiceDetailDAO getLnkPurchaseInvoiceDetail() {
		return lnkPurchaseInvoiceDetail;
	}

	public void setLnkPurchaseInvoiceDetail(
			PurchaseInvoiceDetailDAO lnkPurchaseInvoiceDetail) {
		this.lnkPurchaseInvoiceDetail = lnkPurchaseInvoiceDetail;
	}

	public List<PurchaseInvoiceDetail> getLstPurchaseInvoiceDetail() {
		return lstPurchaseInvoiceDetail;
	}

	public void setLstPurchaseInvoiceDetail(
			List<PurchaseInvoiceDetail> lstPurchaseInvoiceDetail) {
		this.lstPurchaseInvoiceDetail = lstPurchaseInvoiceDetail;
	}

	public List<PurchaseInvoice> getLstPurchaseInvoice() {
		return lstPurchaseInvoice;
	}

	public void setLstPurchaseInvoice(List<PurchaseInvoice> lstPurchaseInvoice) {
		this.lstPurchaseInvoice = lstPurchaseInvoice;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Timestamp getBegin() {
		return begin;
	}

	public void setBegin(Timestamp begin) {
		this.begin = begin;
	}

	public Timestamp getEnd() {
		return end;
	}

	public void setEnd(Timestamp end) {
		this.end = end;
	}

}
